#!/bin/bash
source /Users/frontrow/atvpatch/scripts/common.vars

# DO PRE-INSTALL OPERATIONS #------------------------------------------------
OPERATION="pre-install"
echo "Setting up atvpatch..." > $TMP_LOG; $LOGGER $OPERATION

echo "Making User Directories..." > $TMP_LOG; $LOGGER $OPERATION
mkdir -p /Users/frontrow/{Backup,Documents} 2> $TMP_LOG; $LOGGER
sudo chown -R frontrow:frontrow /Users/frontrow/{Backup,Documents} 2> $TMP_LOG; $LOGGER $OPERATION
mkdir -p /Users/frontrow/Movies/{Movies,Music,Pictures} 2> $TMP_LOG; $LOGGER
sudo chown -R frontrow:frontrow /Users/frontrow/Movies 2> $TMP_LOG; $LOGGER $OPERATION
sudo mkdir -p /mnt/Scratch/Volumes/shared 2> $TMP_LOG; $LOGGER $PACKAGE
echo "Done." > $TMP_LOG; $LOGGER $OPERATION

echo "Making User Directories..." > $TMP_LOG; $LOGGER $OPERATION
sudo mkdir -p /mnt/Scratch/Volumes/shared 2> $TMP_LOG; $LOGGER $PACKAGE
echo "Done." > $TMP_LOG; $LOGGER $OPERATION

echo "Leaving a Trace..." > $TMP_LOG; $LOGGER $OPERATION
touch /Users/frontrow/atvpatch/.patched 2> $TMP_LOG; $LOGGER $OPERATION
echo "Done." > $TMP_LOG; $LOGGER $OPERATION

#echo "Fixing Ownerships..." > $TMP_LOG; $LOGGER $OPERATION
#sudo chown -R frontrow:frontrow /Users/frontrow/atvpatch 2> $TMP_LOG; $LOGGER $OPERATION
#sudo chown -R frontrow:frontrow /Users/frontrow/{Documents,Movies,Music,Pictures} 2> $TMP_LOG; $LOGGER $OPERATION
#echo "Done." > $TMP_LOG; $LOGGER $OPERATION

echo "Disable Auto Update..." > $TMP_LOG; $LOGGER $OPERATION
sudo bash -c 'echo "127.0.0.1       mesu.apple.com" >> /etc/hosts' 2> $TMP_LOG; $LOGGER $OPERATION
echo "Done." > $TMP_LOG; $LOGGER $OPERATION

# Done
unset OPERATION
#----------------------------------------------------------------------------

# INSTALL WEB SERVICES #-----------------------------------------------------
OPERATION="web-services"
echo "Installing Web Services..." > $TMP_LOG; $LOGGER $OPERATION

# Enter Working Directory
cd $PACKAGES_DIR 2> $TMP_LOG; $LOGGER $OPERATION

# Copy
echo "Copying..." > $TMP_LOG; $LOGGER $OPERATION
cp /SeedScratch/Users/frontrow/atvpatch/temp/web-services.rar $PACKAGES_DIR 2> $TMP_LOG; $LOGGER $OPERATION
echo "Done." > $TMP_LOG; $LOGGER $OPERATION

# Unpack
echo "Unpacking..." > $TMP_LOG; $LOGGER $OPERATION
rar x web-services.rar 2> $TMP_LOG; $LOGGER $OPERATION
echo "Done." > $TMP_LOG; $LOGGER $OPERATION

# Install
cd web-services 2> $TMP_LOG; $LOGGER $OPERATION
sudo ./install.sh 2> $TMP_LOG; $LOGGER $OPERATION
cd .. 2> $TMP_LOG; $LOGGER $OPERATION

# Done
unset OPERATION
#----------------------------------------------------------------------------

# INSTALL Packages #---------------------------------------------------------
OPERATION="other-packages"
#echo "### BEGIN-INSTALL-OTHER-PACKAGHES ###" > $TMP_LOG; $LOGGER $OPERATION

sudo /Users/frontrow/atvpatch/scripts/install_basic.sh

# Done
#echo "### END-INSTALL-OTHER-PACKAGHES  ###" > $TMP_LOG; $LOGGER $OPERATION
unset OPERATION
#----------------------------------------------------------------------------

# RESTORE INTRO MOVIE #------------------------------------------------------
OPERATION="post-install"
echo "Restoring Intro Movie..." > $TMP_LOG; $LOGGER $OPERATION

sudo mv /System/Library/CoreServices/Finder.app/Contents/Resources/Intro.m4v /System/Library/CoreServices/Finder.app/Contents/Resources/Intro.m4v.patchstick 2> $TMP_LOG; $LOGGER $OPERATION
sudo mv /System/Library/CoreServices/Finder.app/Contents/Resources/Intro.m4v.bak /System/Library/CoreServices/Finder.app/Contents/Resources/Intro.m4v 2> $TMP_LOG; $LOGGER $OPERATION

# Done
echo "Done." > $TMP_LOG; $LOGGER $OPERATION
unset OPERATION
#----------------------------------------------------------------------------
echo " " > $TMP_LOG; $LOGGER "SYSTEM"
echo "##################################################################" > $TMP_LOG; $LOGGER "SYSTEM"
echo "##################################################################" > $TMP_LOG; $LOGGER "SYSTEM"
echo " " > $TMP_LOG; $LOGGER "SYSTEM"
echo "       Patching done. Thanks for using Patchstick $VERSION" > $TMP_LOG; $LOGGER "SYSTEM"
echo "       System is going down for reboot in 10 seconds..." > $TMP_LOG; $LOGGER "SYSTEM"
echo " " > $TMP_LOG; $LOGGER "SYSTEM"
echo "##################################################################" > $TMP_LOG; $LOGGER "SYSTEM"
echo "##################################################################" > $TMP_LOG; $LOGGER "SYSTEM"
sudo sleep 10
sudo reboot
